﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;
using SDLL;
using DevComponents.DotNetBar.SuperGrid;

namespace SABI
{
    public partial class frm0075 : Form
    {
        string sql = "";
        public frm0075()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHOME"] as frmHOME).CloseTab(this);
        }

        private void frm0075_Load(object sender, EventArgs e)
        {
            
        }

        private void frm0075_Shown(object sender, EventArgs e)
        {
            loadDATA();
        }

        void loadDATA()
        {
            sql = "Select * from tb0044 Order by ObjectTypeID";
            GridDB1.PrimaryGrid.DataSource = FRM.GetDataTable(sql);
        }

        private void GridDB1_SelectionChanged(object sender, DevComponents.DotNetBar.SuperGrid.GridEventArgs e)
        {
            if (GridDB1.PrimaryGrid.SelectedRowCount == 0) { return; }
            GridRow gr = (GridRow)e.GridPanel.ActiveRow;
            txtObjectTypeID.Text = gr.Cells["clObjectTypeID"].Value.ToString();
            txtObjectTypeName.Text = gr.Cells["clObjectTypeName"].Value.ToString();
            chkDisabled.Checked = gr.Cells["clDisabled"].Value.ToString() == "1" ? true : false;
            txtObjectTypeID.ReadOnly = true;
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            nAdd();
        }

        void nAdd()
        {
            txtObjectTypeID.Text = "";
            txtObjectTypeName.Text = "";
            chkDisabled.Checked = false;
            txtObjectTypeID.ReadOnly = false;
        }

        void nDelete()
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1)
            {
                SDLL.DLL02.Alert("Bạn phải chọn dòng cần xóa!");
                return;
            }
            if (!SDLL.DLL02.Confirm("Bạn có chắc muốn xóa?", "Xác nhận")) { return; }

            string sql = "DELETE tb0044 WHERE ObjectTypeID='{0}'";
            sql = string.Format(sql, txtObjectTypeID.Text);
            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString))
            {
                loadDATA();
            }
            else
            {
                SDLL.DLL02.Alert("Xóa dữ liệu không thành công");
            }
        }

        void nInfo()
        {
            string tn = "tb0044";
            string ws = string.Format("WHERE ObjectTypeID='{0}'", txtObjectTypeID.Text);
            Form frm = new frm0067(tn, ws);
            frm.ShowDialog();
        }

        void nRefresh()
        {
            loadDATA();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (!txtObjectTypeID.ReadOnly)
            {
                sql = "INSERT tb0044(ObjectTypeID,ObjectTypeName,Disabled,CreateDate,CreateUserID,LastModifyDate,LastModifyUserID)";
                sql += string.Format(" VALUES('{0}',N'{1}','{2}',GETDATE(),'{3}',GETDATE(),'{3}')", txtObjectTypeID.Text, txtObjectTypeName.Text, chkDisabled.Checked?1:0, FRM.UserName);
                if (DATA.ExecuteNonQuery(sql, FRM.ConnectString))
                {
                    SDLL.DLL02.Info("Lưu dữ liệu thành công");
                    loadDATA();
                }
                else { SDLL.DLL02.Alert("Lưu dữ liệu thất bại"); }
            }
            else
            {
                sql = "UPDATE tb0044 SET ObjectTypeName=N'{0}', Disabled='{1}', LastModifyDate=GETDATE(), LastModifyUserID='{2}' WHERE ObjectTypeID='{3}'";
                sql = string.Format(sql, txtObjectTypeName.Text, chkDisabled.Checked?1:0, FRM.UserName, txtObjectTypeID.Text);
                if (DATA.ExecuteNonQuery(sql, FRM.ConnectString))
                {
                    SDLL.DLL02.Info("Cập nhật dữ liệu thành công");
                    loadDATA();
                }
                else { SDLL.DLL02.Alert("Cập nhật dữ liệu thất bại"); }
            }
        }

        private void mAdd_Click(object sender, EventArgs e)
        {
            nAdd();
        }

        private void mDelete_Click(object sender, EventArgs e)
        {
            nDelete();
        }

        private void mRefresh_Click(object sender, EventArgs e)
        {
            nRefresh();
        }

        private void mSystemInfo_Click(object sender, EventArgs e)
        {
            nInfo();
        }
    }
}
